var MD5 = require('#/static/utils/md5')
var secret='baguznshGu5t9xGARNpq86cd98joQYCN3Cozk1qAbaguznsh'
var domain = 'www.maye.io';
// var host   = 'https://'+domain+'/';
//测试地址
var host ='http://hand.bagudata.com/api/';
function load(method,url,data,success,error){
  ui.request({
    url: host+url, 
    data:data?data:{},
    dataType: 'json',
    method:method,
    header:{"content-type": "application/x-www-form-urlencoded"},
    success: function (res) {
      //成功返回
      success(res.data)
    },
    fail: function (res) {
      error(res)
    },
  })
}
//获取当前时间戳，注意：需要/1000
function getTime(str=''){
  var date = new Date(str);
  var time3 = Date.parse(date);
  return time3;
}

//获取步数
function getStep (){
  ui.onBLECharacteristicValueChange((characteristic) => {
    console.log(ab2hex(characteristic.value));
    var str = ab2hex(characteristic.value)
    ///数据处理
    var type = str.substr(0,2)
    if(type != '03'){
      return
    }
    var time =str.substr(2,8)//时间
    var step =str.substr(10,8)//步数
    var distance = str.substr(18,8)//距离
    var calorie = str.substr(26,8)//卡路里
    var sleep = str.substr(34,6)//睡眠时间
    var array = [time,step,distance,calorie,sleep]
    var list = [];
    array.forEach( s => {
      var g = s.substr(0,2) 
      var s = s.substr(2,2)
      var b = s.substr(4,2)
      var q = s.substr(6,2)
      var sttr = q + b + s + g
      list.push(sttr)
    });
    return list
  });
  
}

module.exports = {
  load: load,
  host: host,
  domain: domain,
  getTime:getTime,
  getStep:getStep
}
